package com.hanj.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 这是注解@ListValue的校验器
 *
 * @author hanj.cn @outlook.com
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(ListValue constraintAnnotation) {
        // 初始化,获取值
        int[] value = constraintAnnotation.value();
        for (int val : value) {
            set.add(val);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        // 判断传进来的值是否在范围内
        return set.contains(value);
    }

}
